#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("无标题记事本");
    QObject::connect(ui->newAction,SIGNAL(triggered()),this,SLOT(newFailSlot()));
    QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFlieSlot()));
    QObject::connect(ui->saveAsAction,SIGNAL(triggered()),this,SLOT(saveAsFileSlot()));
    QObject::connect(ui->saveAction,SIGNAL(triggered()),this,SLOT(saveFileSlot()));
    QObject::connect(ui->exitAction,SIGNAL(triggered()),this,SLOT(exitSlot()));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::newFailSlot()
{
    if(ui->textEdit->document()->isModified())
    {
        qDebug()<<"current file modified";//当前文档已经改变
    }
    else//当前文件还没有改变
    {
   //qDebug()<<"the file is not modified";
    //clear();
    //this->setWindowFileTitle("unmodified");
      ui->textEdit->clear();
        this->setWindowTitle("Untitled.txt");
    }
}
void MainWindow::openFlieSlot()
{
    QString fileName=QFileDialog::getOpenFileName(this,"Open Flie",QDir::currentPath());
   // qDebug()<<"fileName is"<<fileName;
    if(fileName.isEmpty())
{
    QMessageBox::information(this,"Error","Please select a  Text");
    return; 
}
    QFile *file=new QFile;
    file->setFileName(fileName);//set file name
    bool ok=file->open(QIODevice::ReadOnly);
    //open file as read only mode
     if(ok)
    {
         //文件与文本流相关联
            QTextStream in(file);//读入文本流 和file相关联
            //in有个readAll方法  in.readAll();//readall context from the file
           // in.readall()就像一个存储着文件文档内容的变量
            ui->textEdit->setText(in.readAll());//写入in.readAll（）里面所读取的数据
            file->close();
            delete file;
     }

}

void MainWindow::saveFileSlot()
{

if(saveFileName.isEmpty())
{
    this->saveAsFileSlot();
}
else
{
    QFile *file=new QFile;
    file->setFileName(saveFileName);
bool  ok=file-> open(QIODevice::WriteOnly);
        if(ok)
{
        QTextStream out(file);
        //cout<<
       out<< ui->textEdit->toPlainText();//这里是取出textEdit中的纯文本
     //  ui->textEdit->toPlainText();这里是取出textEdit中的纯文本
       //out的意思是将数据写入数据流
        file->close();
        delete file;
}
}
}
void MainWindow::saveAsFileSlot()
{
    QString saveFileName=QFileDialog::getSaveFileName(this,"save file",QDir::currentPath());
    //getSaveFileName
    if(saveFileName.isEmpty())
    {
        QMessageBox::information(this,"Error Message","please select a text");
       return;
  }
    QFile *file=new QFile;
    file->setFileName(saveFileName);
bool  ok=file-> open(QIODevice::WriteOnly);
        if(ok)
{
        QTextStream out(file);
        //cout<<
       out<< ui->textEdit->toPlainText();//这里是取出textEdit中的纯文本
     //  ui->textEdit->toPlainText();这里是取出textEdit中的纯文本
       //out的意思是将数据写入数据流
        file->close();
        delete file;
}
else
{
            QMessageBox::information(this,"Error Message","Save Flie Error");
            return;
        }
}
void MainWindow::exitSlot()
{
   this->  close();
}
void MainWindow::setFontSlot()
{



}
